package com.haynes.meetxiaohai.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义元数据对象处理器(对一些公共字段值进行填充，比如创建人、创建时间、更新人、更新时间)
 * @Author Haynes
 * @Date 2023/3/20 8:14
 * @PackageName:com.haynes.reggie.common
 * @ClassName: MyMetaObjectHandler
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 添加时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段填充》》》》Insert");
        log.info("线程中ID的值"+BaseContext.getCurrent());

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrent());
        metaObject.setValue("updateUser", BaseContext.getCurrent());
    }

    /**
     * 更新时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段填充》》》》Update");
        log.info("线程中ID的值"+BaseContext.getCurrent());

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrent());
    }
}
